/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.amazon.aws.partners.saasfactory.saasboost;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;
import java.util.Properties;

@JsonDeserialize(builder = GitVersionInfo.Builder.class)
public final class GitVersionInfo {
    private static final Logger LOGGER = LoggerFactory.getLogger(GitVersionInfo.class);

    // VisibleForTesting
    public static final String TAG_NAME_PROPERTY = "git.closest.tag.name";
    public static final String COMMIT_HASH_PROPERTY = "git.commit.id.abbrev";
    public static final String DESCRIPTION_PROPERTY = "git.commit.id.describe-short";

    private final String tag;
    private final String commit;
    private final String describe;

    private GitVersionInfo(Builder b) {
        this.tag = b.tag;
        this.commit = b.commit;
        this.describe = b.describe;
    }

    public String getTag() {
        return tag;
    }

    public String getCommit() {
        return commit;
    }

    public String getDescribe() {
        return describe;
    }

    @Override
    public boolean equals(Object other) {
        if (!(other instanceof GitVersionInfo)) {
            return false;
        }
        GitVersionInfo otherVersionInfo = (GitVersionInfo) other;
        return getTag().equals(otherVersionInfo.getTag())
            && getCommit().equals(otherVersionInfo.getCommit())
            && getDescribe().equals(otherVersionInfo.getDescribe());
    }

    @Override
    public int hashCode() {
        return Objects.hash(tag, commit, describe);
    }

    public static Builder builder() {
        return new Builder();
    }

    public static GitVersionInfo fromProperties(Properties gitProperties) {
        /* e.g.
         *  #Generated by Git-Commit-Id-Plugin
         *  git.closest.tag.name=v2.0.0
         *  git.commit.id.abbrev=18cc13b
         *  git.commit.id.describe-short=v2.0.0-2-dirty
         */
        return builder()
            .tag(gitProperties.getProperty(TAG_NAME_PROPERTY))
            .commit(gitProperties.getProperty(COMMIT_HASH_PROPERTY))
            .describe(gitProperties.getProperty(DESCRIPTION_PROPERTY))
            .build();
    }

    @JsonPOJOBuilder(withPrefix = "") // setters aren't named with[Property]
    public static class Builder {
        private String tag;
        private String commit;
        private String describe;

        private Builder() {

        }

        public Builder tag(String tag) {
            this.tag = tag;
            return this;
        }

        public Builder commit(String commit) {
            this.commit = commit;
            return this;
        }

        public Builder describe(String describe) {
            this.describe = describe;
            return this;
        }

        public GitVersionInfo build() {
            if (tag == null || commit == null || describe == null) {
                LOGGER.error("Invalid attempted construction of git version information."
                        + "tag: {}, commit: {}, describe: {}", tag, commit, describe);
                throw new IllegalArgumentException("Invalid attempted construction of git version information.");
            }
            return new GitVersionInfo(this);
        }
    }
}
